//返回 例 2017
const formatYear = data => {
    var date = data.date;
    var types = data.types;
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    if (types == 'year') {
        return [year].map(formatNumber).join('-')
    } else if (types == 'month') {
        return [month].map(formatNumber).join('-')
    } else if (types == 'day') {
        return [day].map(formatNumber).join('-')
    }

}

//返回 例 12月02日
const formatDate2 = date => {
    const month = date.getMonth() + 1
    const day = date.getDate()
    return [month].map(formatNumber) + '月' + [day].map(formatNumber) + '日'
}

//返回 例 2017-12-12
const formatDate = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    return [year, month, day].map(formatNumber).join('-')
}
//返回 例 2017-12-12 12:30:00
const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
//返回 例 12-12 12:30:00
const formatTime2 = date => {
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()

    return [month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}
//返回 小时12:00:00
const formatHour = data => {
    var date = data.date;
    var types = data.types;
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    if (types == 'hour') {
        return [hour].map(formatNumber)
    } else if (types == 'minute') {
        return [minute].map(formatNumber).join('-')
    } else if (types == 'second') {
        return [second].map(formatNumber)
    }
}

//返回 指定秒数59 ，  2017-12-12 12:30:59
function selfFormatTimeReturnSecond59(time) {
    let date = new Date(time);
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    let hour = date.getHours()
    let minute = date.getMinutes()
    let second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, '59'].map(formatNumber).join(':')
}

//返回 2017-12-12 12:30:59
function selfFormatTimeYMDHMS(time) {
    let date = new Date(time);
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    let hour = date.getHours()
    let minute = date.getMinutes()
    let second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}


//返回 2017-12-12 12:30
function selfFormatTimeYMDHM(time) {
    let date = new Date(time);
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    let hour = date.getHours()
    let minute = date.getMinutes()
    let second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}

//返回 2017-12-12 12
function selfFormatTimeYMDH(time) {
    let date = new Date(time);
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    let hour = date.getHours()
    let minute = date.getMinutes()
    let second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour].map(formatNumber).join(':')
}

//返回 例 2017-12-12
function selfFormatTimeYMD(time) {
    let date = new Date(time)
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    let hour = date.getHours()
    let minute = date.getMinutes()
    let second = date.getSeconds()
    return [year, month, day].map(formatNumber).join('-')
}

//返回 12:30
function selfFormatTimeHM(time) {
    let date = new Date(time);
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    let hour = date.getHours()
    let minute = date.getMinutes()
    let second = date.getSeconds()

    return [hour, minute].map(formatNumber).join(':')
}

/**
 * JS 计算两个时间间隔多久（时分秒）
 * @param startTime "2019-10-23 15:27:23"
 * @param endTime "2019-10-23 15:27:55"
 * @return 1天2时3分5秒
 */
function twoTimeInterval(startTime, endTime) {
    // 开始时间
    let date1 = new Date(startTime);
    // 结束时间
    let date2 = new Date(endTime);
    // 时间相差秒数
    let dateDiff = date2.getTime() - date1.getTime();
    // 计算出相差天数
    let days = Math.floor(dateDiff / (24 * 3600 * 1000));
    // 计算出小时数
    let residue1 = dateDiff % (24 * 3600 * 1000); // 计算天数后剩余的毫秒数
    let hours = Math.floor(residue1 / (3600 * 1000));
    // 计算相差分钟数
    let residue2 = residue1 % (3600 * 1000); // 计算小时数后剩余的毫秒数
    let minutes = Math.floor(residue2 / (60 * 1000));
    // 计算相差秒数
    let residue3 = residue2 % (60 * 1000); // 计算分钟数后剩余的毫秒数
    let seconds = Math.round(residue3 / 1000);
    let returnVal =
        ((days == 0) ? "" : days + "天") +
        ((hours == 0) ? "" : hours + "时") +
        ((minutes == 0) ? "" : minutes + "分")
    // + ((seconds == 0) ? "" : seconds + "秒");
    return returnVal;
}


/**
 * JS 计算两个时间间隔多久（时）
 * @param startTime "2019-10-23 15:27:23"
 * @param endTime "2019-10-23 15:27:55"
 * @return 12时
 */
function twoTimeIntervalReturnHours(startTime, endTime) {
    // 开始时间
    let date1 = new Date(startTime);
    // 结束时间
    let date2 = new Date(endTime);
    // 时间相差秒数
    let dateDiff = date2.getTime() - date1.getTime();
    // 计算出小时数
    let hours = Math.floor(dateDiff / (3600 * 1000));
    return hours;
}

const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}

// 数组sum合计
function selfArrSum(list) {
    let count = 0;
    list.map(item => {
        count = count + item
    })
    return count
}

// 返回url参数对象
function getUrlParamsObj(url) {
    // 通过 ? 分割获取后面的参数字符串
    let urlStr = url.split('?')[1]
    // 创建空对象存储参数
	let obj = {};
    // 再通过 & 将每一个参数单独分割出来
	let paramsArr = urlStr.split('&')
	for(let i = 0,len = paramsArr.length;i < len;i++){
        // 再通过 = 将每一个参数分割为 key:value 的形式
		let arr = paramsArr[i].split('=')
		obj[arr[0]] = arr[1];
	}
	return obj
}

module.exports = {
    formatTime: formatTime,
    formatDate: formatDate,
    formatYear: formatYear,
    formatDate2: formatDate2,
    formatHour: formatHour,
    formatTime2: formatTime2,
    selfFormatTimeReturnSecond59,
    selfFormatTimeYMDHMS,
    selfFormatTimeYMDHM,
    selfFormatTimeYMD,
    selfFormatTimeHM,
    selfFormatTimeYMDH,
    twoTimeInterval,
    twoTimeIntervalReturnHours,
    selfArrSum,
    getUrlParamsObj
}